package com.example.book_recommend.algorithm;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel;
import org.apache.mahout.cf.taste.impl.model.jdbc.ReloadFromJDBCDataModel;
import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
import org.apache.mahout.cf.taste.model.JDBCDataModel;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;

@Component
public class SchedueConfig {
    public static Recommender recommender;
    ReloadFromJDBCDataModel dataModel;
    ItemSimilarity itemSimilarity;
    @Scheduled(cron="0/10 * * * * ?")
    public void executeFileDownLoadTask() throws TasteException, IOException, ClassNotFoundException {
        System.out.println("定时任务启动");
        String driver = "com.mysql.jdbc.Driver";
        MysqlDataSource mysqlDataSource = new MysqlDataSource();
        mysqlDataSource.setServerName("124.70.109.245");
//        mysqlDataSource.setServerName("localhost");
        mysqlDataSource.setUser("root");
        mysqlDataSource.setPassword("123456");
        mysqlDataSource.setDatabaseName("books");
        Class.forName(driver);
        JDBCDataModel jdbcDataModel = new MySQLJDBCDataModel(mysqlDataSource,
                "preference",
                "user_id",
                "item_id",
                "preference",
                null);
        dataModel = new ReloadFromJDBCDataModel(jdbcDataModel);
        // 构造数据模型，计算内容相似度
        itemSimilarity = new PearsonCorrelationSimilarity(dataModel);
        // 构造推荐引擎
        recommender = new GenericItemBasedRecommender(dataModel, itemSimilarity);
//        // 得到推荐的结果
//        List<RecommendedItem> recommendedItemList = recommender.recommend(userId, recommendNum);
//        for (RecommendedItem recommendedItem : recommendedItemList) {
//            long itemID = recommendedItem.getItemID();
//            float value = recommendedItem.getValue();
//            System.out.println("itemID:" + itemID + "  --------------  " +  "value:" + value);
//        }


//        /**
//         * 用户偏好数据包含评分
//         欧氏距离：EuclideanDistanceSimilarity
//         皮尔森距离：PearsonCorrelationSimilarity
//         余弦距离：UncenteredCosineSimilarity
//
//         用户偏好数据不包含评分
//         曼哈顿距离：CityBlockSimilarity
//         对数似然距离： LogLikelihoodSimilarity
//         */
//        UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
//        // 相邻用户UserNeighborhood
//        /**
//         * NearestNUserNeighborhood
//         指定距离最近的N个用户作为邻居。
//         示例：UserNeighborhood unb = new NearestNUserNeighborhood(10, us, dm);
//         三个参数分别是： 邻居的个数，用户相似度，数据模型
//         邻居个数的大小直接决定了推荐结果的近似程度和计算的复杂度
//         ThresholdUserNeighborhood
//         指定距离最近的一定百分比的用户作为邻居。
//         示例：UserNeighborhood unb = new ThresholdUserNeighborhood(0.2, us, dm);
//         三个参数分别是： 阀值（取值范围0到1之间），用户相似度，数据模型
//         */
//        UserNeighborhood neighborhood = new NearestNUserNeighborhood(500, similarity, model);
//        //根据数据模型、用户相似度模型、以及邻近值构建推荐引擎
//        Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
//        // 向用户100推荐2个商品
//        List<RecommendedItem> recommendations = recommender.recommend(100, 2);
//        for (RecommendedItem recommendation : recommendations) {
//            // 输出推荐结果
//            System.out.println(recommendation);
//        }

    }
}

